#include "linechart.h"

LineChart::LineChart(QWidget *parent)
    : QWidget(parent)
{
	mBackground = QBrush(QColor(0xff,0xee, 0xee));
	mTitleFont = QFont();
	mTitleFont.setPointSize(14);
	mAxisFont = QFont();
	mAxisFont.setPointSize(10);
	mTitleFontMetrics = new QFontMetrics(mTitleFont);
	mAxisFontMetrics = new QFontMetrics(mAxisFont);
	mPlotter = new Plotter();
}

LineChart::~LineChart()
{
	delete mPlotter;
	delete mTitleFontMetrics;
	delete mAxisFontMetrics;
}

void LineChart::paintEvent(QPaintEvent *event)
 {
     QPainter painter;
     painter.begin(this);
     painter.setRenderHint(QPainter::Antialiasing);

     int titleHeight = 50;
     int leftMargin = 30;
     int bottomMargin = 16;

     QRect bounds = QRect(QPoint(leftMargin, titleHeight), QPoint(width(), height() - bottomMargin));

     QRect titleRect = QRect(QPoint(leftMargin,0), QPoint(width(), titleHeight));
     painter.setFont(mTitleFont);
     painter.drawText(titleRect, Qt::AlignCenter  | Qt::TextWordWrap, mChartTitle);

     QRect horizontalTitleRect = QRect(QPoint(leftMargin, height()-bottomMargin), QPoint(width(), height()));
     painter.setFont(mAxisFont);
     painter.drawText(horizontalTitleRect, Qt::AlignCenter | Qt::TextWordWrap, mHorizontalTilte);

     painter.save();
     painter.translate(QPoint(leftMargin-10, (bounds.height() + mAxisFontMetrics->width(mVerticalTitle))/2 + titleHeight));
     painter.rotate(270);
     painter.drawText(0, 0, mVerticalTitle);
     painter.restore();

     //Perform paint
     painter.resetTransform();
     painter.translate(QPoint(leftMargin, titleHeight));


     mPlotter->plot(&painter, bounds, mDataset);

     painter.end();
}

void LineChart::setHorizontalTitle(QString t) {
	mHorizontalTilte = t;
}

void LineChart::setVerticalTitle(QString t) {
	mVerticalTitle = t;
}

void LineChart::setDataset(CategoryDataSet* dataset) {
	mDataset = dataset;
}

void LineChart::setTitle(QString t) {
	mChartTitle = t;
}
